package com.kevin.mapping.script.node.logical;

import com.kevin.mapping.script.engine.JSONEnv;
import com.kevin.mapping.script.exception.ExecuteException;
import com.kevin.mapping.script.lexer.BoolValue;
import com.kevin.mapping.script.lexer.Token;
import com.kevin.mapping.script.lexer.Type;
import com.kevin.mapping.script.node.stmt.Stmt;

public class Or extends Logical {


    public Or(Token token, Stmt left, Stmt right) {
        super(token, left, right);
    }

    @Override
    public Token execute(JSONEnv env) {
        Token leftRet = this.getLeft().execute(env);
        Token rightRet = this.getRight().execute(env);
        int type = Math.max(leftRet.type, rightRet.type);
        switch (type) {
            case Type.BOOLEAN:
                return new BoolValue(Type.INTEGER, (Boolean) leftRet.getValue(env) || (Boolean) rightRet.getValue(env));
            default:
                throw new ExecuteException("unsuported type" + type);
        }
    }
}
